<?php
declare (strict_types = 1);

namespace app\api\controller;
use app\middleware\Auth;
use app\model\RedisModel;
use app\model\UserAddressesModel;
use think\App;

class UserAddress extends Base {

    protected $middleware = [Auth::class];

    private $userAddressModel;

    private $req;

    private $uid;

    private $cacheKey="user:addresses:id:";


    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->userAddressModel = new UserAddressesModel();

        $this->req = request()->param();

        $this->header = request()->header();

        $this->uid = $this->isLogin();

    }



    /*
     * 添加地址
     * */
    public function addUserAddress(){

        if(empty($this->req['province'])){
            return $this->fail("省份不能为空");
        }

        if(empty($this->req['city'])){
            return $this->fail("市不能为空");
        }


        if(empty($this->req['county'])){
            return $this->fail("区不能为空");
        }

        if(empty($this->req['address'])){
            return $this->fail("详细地址不能为空");
        }

        if(empty($this->req['name'])){
            return $this->fail("收件人不能为空");
        }

        if(!verification_mobile($this->req['mobile'])){
            return $this->fail("手机号不合法");
        }

        $data = [
            'uid' => $this->uid,
            'province' => $this->req['province'],
            'city' => $this->req['city'],
            'county' => $this->req['county'],
            'address' => $this->req['address'],
            'name' => $this->req['name'],
            'mobile' => $this->req['mobile'],
        ];

        if($this->userAddressModel->getTotal($data)){
            return $this->fail("地址已经存在");
        }

        if($this->userAddressModel->getUserAddressNum($this->uid) < 1){
            $data['is_default'] = 1;
        }else{
            $data['is_default'] = isset($this->req['is_default']) ? $this->req['is_default'] : 0;
        }


        //删除用户默认地址
        if($data['is_default']){
            $this->userAddressModel->cancelUserDefaultAddress($this->uid);
        }

        //添加地址
        $res = $this->userAddressModel->addOne($data);

        if($res){

            $this->delUserCache();

            return $this->succ();

        }else{

            return $this->fail();

        }

    }



    /*
     * 修改地址
     * */
    public function editUserAddress(){

        if(empty($this->req['address_id'])){
            return $this->fail("参数错误：address_id 不能为空");
        }

        if(empty($this->req['province'])){
            return $this->fail("省份不能为空");
        }

        if(empty($this->req['city'])){
            return $this->fail("市不能为空");
        }

        if(empty($this->req['county'])){
            return $this->fail("区不能为空");
        }

        if(empty($this->req['address'])){
            return $this->fail("详细地址不能为空");
        }

        if(empty($this->req['name'])){
            return $this->fail("收件人不能为空");
        }

        if(!verification_mobile($this->req['mobile'])){
            return $this->fail("手机号不合法");
        }
        if(!$this->userAddressModel->isUserAddress($this->uid,$this->req['address_id'])){
            return $this->fail("地址不存在");
        }

        $data = [
            'uid' => $this->uid,
            'province' => $this->req['province'],
            'city' => $this->req['city'],
            'county' => $this->req['county'],
            'address' => $this->req['address'],
            'name' => $this->req['name'],
            'mobile' => $this->req['mobile'],
        ];

        $where=[];
        $where[] = ["uid","=",$this->uid];
        $where[] = ["province","=",$this->req['province']];
        $where[] = ["city","=",$this->req['city']];
        $where[] = ["county","=",$this->req['county']];
        $where[] = ["address","=",$this->req['address']];
        $where[] = ["name","=",$this->req['name']];
        $where[] = ["mobile","=",$this->req['mobile']];
        $where[] = ["address_id","<>",$this->req['address_id']];

        if($this->userAddressModel->getTotal($where)){
            return $this->fail("已存在相同地址");
        }

        $data['is_default'] = isset($this->req['is_default']) ? $this->req['is_default'] : 0;

        //删除用户默认地址
        if($data['is_default']){
            $this->userAddressModel->cancelUserDefaultAddress($this->uid);
        }

        //添加地址
        $where=[];
        $where['address_id'] = $this->req['address_id'];
        $res = $this->userAddressModel->updateAll($where,$data);

        if($res){

            $this->delUserCache();

            return $this->succ();

        }else{

            return $this->fail();

        }

    }


    /*
 *  获取用户地址列表
 * */
    public function getUserAddressInfo()
    {

        if(empty($this->req['address_id'])){
            return $this->fail("参数错误：address_id 不能为空");
        }


        if(!$this->userAddressModel->isUserAddress($this->uid,$this->req['address_id'])){
            return $this->fail("地址不存在");
        }

        $data = $this->userAddressModel->getInfo($this->req['address_id']);

        return $this->succ($data);
    }





    /*
     *  获取用户地址列表
     * */
    public function getUserAddressList()
    {
        $total = $this->userAddressModel->getUserAddressNum($this->uid);
        $data=[];
        if($total){
            $data = $this->userAddressModel->getUserAddressList($this->uid);
        }

        return $this->succData($total,$data);
    }


    /*
     *  获取用户默认地址
     * */
    public function getUserDefaultAddress()
    {
        $info = $this->userAddressModel->getUserDefaultAddressInfo($this->uid);

        return $this->succ($info);
    }




    /*
   *  绑定用户地址为默认地址
   * */
    public function bindUserDefaultAddress()
    {
        if(empty($this->req['address_id'])){
            return $this->fail("参数错误：address_id 不能为空");
        }

        if($this->userAddressModel->bindUserDefaultAddress($this->uid,$this->req['address_id'])){

            $this->delUserCache();

            return $this->succ();

        }else{

            return $this->fail();

        }

    }

    /*
       * 删除地址
       * */

    public function delUserAddress(){
        if(empty($this->req['address_ids'])){
            return $this->fail("参数错误：address_id 不能为空");
        }

        $res = $this->userAddressModel->delUserAddress($this->uid,$this->req['address_ids']);

        if($res){
            $this->delUserCache();
            return $this->succ();

        }else{

            return $this->fail();

        }
    }

    /*
     * 删除地址
     * */

    public function delUserCache(){
        $total = $this->userAddressModel->getUserAddressNum($this->uid);
        if($total){
            $data = $this->userAddressModel->getUserAddressList($this->uid);
            foreach ($data as $item){
                $this->delCache($item['address_id']);
            }
        }
    }



    public function delCache($id)
    {
        $redisModel = new RedisModel();

        return $redisModel->delete($this->cacheKey.$id);
    }


    public function delAllCache()
    {
        $redisModel = new RedisModel();

        return $redisModel->deleteAll($this->cacheKey);
    }
}
